﻿using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class BVVDLibrary
    {
        public string encryptPassword(string input)
        {
            SHA512 sha = new System.Security.Cryptography.SHA512Managed();
            byte[] password = System.Text.Encoding.Default.GetBytes(input);
            byte[] result = sha.ComputeHash(password);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X"));
            }

            return sb.ToString();

        }

        public int countPT_TS(string makt)
        {
            BVVDDataContext db = new BVVDDataContext();
            int num  = (from ps in db.F_PTHI_Ts
                          where ps.MAKT.Equals(makt)
                          select ps).ToList().Count;
            return num;


        }

        public int countSBD(string makt) {
            BVVDDataContext db = new BVVDDataContext();
            int num = (from sbd in db.F_SOBAODANHs
                         where sbd.MAKT.Equals(makt)
                         select sbd).ToList().Count;
            return num;
        
        }

        public int countTS(string makt) {
            BVVDDataContext db = new BVVDDataContext();
            int num = (from ts in db.LST_THISINHs
                       where ts.MAKT.Equals(makt)
                       select ts).ToList().Count;
            return num;
        
        
        }

        public int countPT(string makt) {
            BVVDDataContext db = new BVVDDataContext();
            int num = (from pt in db.LST_PHONGTHIs
                       where pt.MAKT.Equals(makt)
                       select pt).ToList().Count;
            return num;
            
        }

        public int countDT_TS(string makt) {
            BVVDDataContext db = new BVVDDataContext();
            int num = (from ds in db.F_DIEMTHI_Ts
                       where ds.MAKT.Equals(makt)
                       select ds).ToList().Count;

            return num;
        
        }

        public void createPT_TS(string makt) {
            BVVDDataContext db = new BVVDDataContext();
            int num = 0;
            var listPT = (from pt in db.LST_PHONGTHIs
                          where pt.MAKT.Equals(makt)
                          select pt).OrderBy(a=>a.MAPT).ToList();
            //num = listPT.Count();
            for (int i = 0; i < listPT.Count; i++)
			{
                
                var listSBD = (from sbd in db.F_SOBAODANHs
                              where sbd.MAKT.Equals(makt)
                              select sbd).Skip(i * listPT[0].SOTS.Value).Take(listPT[i].SOTS.Value ).ToList();
                foreach(F_SOBAODANH ts in listSBD){
                    F_PTHI_T ps = new F_PTHI_T
                    {
                        MAKT = makt,
                        MAPT = listPT[i].MAPT,
                        SOBAODANH = ts.SOBAODANH
                    };
                   // num++;
                    db.F_PTHI_Ts.InsertOnSubmit(ps);
                    db.SubmitChanges();
                
                }

			}


           // return num;
        }

    }
}
